﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/10/2009
 * Time: 11:38 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using NHibernate;
using System.Collections.Generic;
using System.Data;
using Buaya.Model.Dao.Interface;

namespace Buaya.Model.Dao.NHibernate
{
	/// <summary>
	/// Description of NHibernateDeductionDao.
	/// </summary>
	public class NHibernateDeductionDao : IDeductionDao
	{
		public NHibernateDeductionDao()
		{
		}
		
		public void SaveOrUpdate(Deduction deduction)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.SaveOrUpdate(deduction);
			session.Flush();
		}
		
		public void Delete(Deduction deduction)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.Delete(deduction);
			session.Flush();
		}
		
		public Deduction ReadByPk(int id)
		{
			return NHibernateHelper.OpenSession().Load<Deduction>(id);
		}
		
		public IList<Deduction> FindAll()
		{
			return NHibernateHelper.OpenSession().CreateCriteria(typeof(Deduction)).List<Deduction>();
		}
		
		public IList<Deduction> FindByEmployeeAndPayrollCode(Employee e, PayrollCode c)
		{
			IQuery query = NHibernateHelper.OpenSession().CreateQuery("from Deduction where EmployeeId = :empId and PayrollCodeId = :payrollCodeId");
			query.SetInt32("empId", e.Id);
			query.SetInt32("payrollCodeId", c.Id);
			return query.List<Deduction>();
		}
	}
}
